import variables from '@/assets/styles/element-variables.scss'
import defaultSettings from '@/settings'
import { getStorage, setStorage } from '@/utils/store'

const { showSettings, tagsView, fixedHeader, sidebarLogo, topNav } = defaultSettings

const state = {
  theme: getStorage('theme') ? getStorage('theme') : variables.theme,
  showSettings: getStorage('showSettings') ? getStorage('showSettings') !== 'false' : showSettings,
  tagsView: getStorage('tagsView') ? getStorage('tagsView') === 'true' : tagsView,
  fixedHeader: getStorage('fixedHeader') ? getStorage('fixedHeader') === 'true' : fixedHeader,
  sidebarLogo: getStorage('sidebarLogo') ? getStorage('sidebarLogo') !== 'false' : sidebarLogo,
  topNav: getStorage('topNav') ? getStorage('topNav') === 'true' : topNav
}

const mutations = {
  CHANGE_SETTING: (state, { key, value }) => {
    // eslint-disable-next-line no-prototype-builtins
    if (state.hasOwnProperty(key)) {
      state[key] = value
      setStorage(key, value)
    }
  }
}

const actions = {
  changeSetting({ commit }, data) {
    commit('CHANGE_SETTING', data)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

